﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class UserWork_MailSend : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
           con.Open();
           string sql = "select * from Branch";
           SqlCommand com = new SqlCommand(sql,con);
           SqlDataReader dr = com.ExecuteReader();
           while (dr.Read())
           {
               DropDownList1.Items.Add(new ListItem(dr["Branch"].ToString(), dr["Dutyid"].ToString()));
           }
           DropDownList1.Items.Insert(0,"选择部门....");
            
           con.Close();



           SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
           con1.Open();
           string sql1 = "select * from Employee";
           SqlDataAdapter da1 = new SqlDataAdapter(sql1, con1);
           DataSet ds1 = new DataSet();
           da1.Fill(ds1);
           ListBox1.DataSource = ds1;
           ListBox1.DataTextField = "Name";
           ListBox1.DataValueField = "username";
           ListBox1.DataBind();
           con1.Close();
       }

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string Dutyid = DropDownList1.SelectedItem.Value.ToString();
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            string sql = "select Name,username from Employee  where dutyid="+Dutyid+"";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            ListBox1.DataSource = ds;
            ListBox1.DataTextField = "Name";
            ListBox1.DataValueField = "username";
            ListBox1.DataBind();
            con.Close();
     

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        bool exist = false;
        foreach (ListItem li in this.ListBox2.Items)
        {
            if (li.Value == this.ListBox1.SelectedItem.Value)
            {
                exist = true;
            }
        }
        if (exist == false)
        {
            this.ListBox2.Items.Add(new ListItem(this.ListBox1.SelectedItem.Text, this.ListBox1.SelectedItem.Value));
        }


         
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < ListBox2.Items.Count; i++)
        {
            if (this.ListBox2.Items[i].Selected==true)
            {
                ListBox2.Items.Remove(this.ListBox2.Items[i]);
                
            }
           // ListBox2.Items[i].Text
        }
    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
     
        foreach (ListItem li in ListBox1.Items)
        {
            bool exist = false;
            foreach (ListItem lised in ListBox2.Items)
                if (lised.Value ==li.Value)
                    exist = true;
            if (exist == false)
                ListBox2.Items.Add(new ListItem(li.Text,li.Value));
        }

       
        //for (int i = 0; i < ListBox1.Items.Count; i++)
        //{
        //    for (int j = 0; j< ListBox2.Items.Count; j++)
        //    {
        //        if (ListBox2.Items[j].Text.ToString() == ListBox1.SelectedItem.Text.ToString())
        //        {
        //            Response.Write("<script>alert('不能添家相同员工!')</script>");
        //            return;
        //        }
        //    }


        //    ListBox2.Items.Add(ListBox1.Items[i].Text.ToString());
        //}
    }
    protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
    {
      
        for (int i = 0; i < ListBox2.Items.Count;)
        {
            ListBox2.Items.Remove(ListBox2.Items[i]);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.close();</script>");
    }
    public string pass()
    {
        string txt = "";
        for (int j = 0; j < this.ListBox2.Items.Count; j++)
        {
            txt += this.ListBox2.Items[j];
            txt += ";";

        }
        return txt;

    }
}
